﻿using Elastic.Channels;
using Elastic.CommonSchema.Serilog;
using Elastic.Ingest.Elasticsearch;
using Elastic.Ingest.Elasticsearch.DataStreams;
using Elastic.Serilog.Sinks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
//using Serilog.Sinks.Elasticsearch;
using System;
using System.IO;

namespace ProductService.Host
{
    public class Program
    {
        public static int Main(string[] args)
        {
            //TODO: Temporary: it's not good to read appsettings.json here just to configure logging
            var currentEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");


            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{currentEnv}.json", optional: true)
                .AddEnvironmentVariables()
                .Build();
            Console.WriteLine($"当前环境--{currentEnv}--");
            //Log.Logger = new LoggerConfiguration()
            //    .MinimumLevel.Debug()
            //    .MinimumLevel.Override("Microsoft", LogEventLevel.Error)
            //    .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Error)
            //    .MinimumLevel.Override("Volo.Abp", LogEventLevel.Error)
            //    .Enrich.WithProperty("Application", "ProductService")
            //    .Enrich.FromLogContext()
            //    //.WriteTo.Console()
            //    .WriteTo.Elasticsearch(new[] { new Uri(configuration["ElasticSearch:Url"]) }, opts =>
            //    {
            //        opts.DataStream = new DataStreamName($"ProductService.Host", currentEnv, DateTime.Now.ToString("yyyy-MM-dd"));
            //        opts.BootstrapMethod = BootstrapMethod.Failure;

            //        opts.TextFormatting = new EcsTextFormatterConfiguration
            //        {
            //            MapCustom = (e, _) => e
            //        };
            //        opts.ConfigureChannel = channelOpts =>
            //        {
            //            channelOpts.BufferOptions = new BufferOptions
            //            {
            //                ExportMaxConcurrency = 10
            //            };
            //        };
            //    })
            //    .CreateLogger();

            try
            {
                Log.Information("Starting ProductService.Host.");
                CreateHostBuilder(args).Build().Run();
                return 0;
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "ProductService.Host terminated unexpectedly!");
                return 1;
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        internal static IHostBuilder CreateHostBuilder(string[] args) =>
            Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
                .UseAutofac()
                .UseSerilog();
    }
}
